Passed
Push — master ( eee053...2b5061 )
by Joe Nilson
04:47
created

CommonDomFunctions.js ➔ saveBussinessDocument   B

Complexity

Conditions 5

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
c 0
b 0
f 0
dl 0
loc 33
rs 8.8613
1
/*
2
 * Copyright (C) 2021 Joe Nilson <[email protected]>
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as
6
 * published by the Free Software Foundation, either version 3 of the
7
 * License, or (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 * You should have received a copy of the GNU Lesser General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
17
/**
18
 * @param {string} tipoComprobante
19
 * @returns {Promise<*>}
20
 */
21
async function verificarCorrelativoNCF(tipoComprobante, tipoOperacion)
22
{
23
    let ArrayTipoNCFCompras = ['11','12','16','17'];
24
    if (tipoOperacion === 'Compras' && !ArrayTipoNCFCompras.includes($("#doc_codsubtipodoc").val())) {
25
        return true;
26
    }
27
28
    return $.ajax({
29
        url: 'ListNCFRango',
30
        async: true,
31
        data: {'action': 'busca_correlativo', 'tipocomprobante': tipoComprobante},
32
        type: 'POST',
33
        datatype: 'json',
34
        success: function (response) {
35
            let data = JSON.parse(response);
36
            if ( data.existe === false ) {
37
                executeModal(
38
                    'verificaNCF',
39
                    'No hay Correlativo de NCF Disponible',
40
                    'No hay correlativos disponibles para el Tipo de NCF ' +
41
                    tipoComprobante + ' <br/>Por favor revise su maestro de NCFs',
42
                    'warning'
43
                );
44
            }
45
        },
46
        failure: function (response) {
0 ignored issues
show
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
47
            alert('Ha ocurrido algún tipo de falla ' + status);
0 ignored issues
show
Bug introduced by
The variable status seems to be never declared. If this is a global, consider adding a /** global: status */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
48
        },
49
        error: function (xhr, status) {
50
            alert('Ha ocurrido algún tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
51
        }
52
    });
53
}
54
55
/**
56
 * logConsole in Debug mode
57
 * @param value
58
 * @param description
59
 */
60
function logConsole(value, description ='data')
61
{
62
    if ($(".debugbar") !== undefined) {
63
        console.log(description, value);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
64
    }
65
}
66
67
/********
68
 * Util Functions
69
 */
70
71
/**
72
 *
73
 * @param {object} btn
74
 * @param {string} text
75
 */
76
function setLoadingButton(btn, text)
77
{
78
    $(btn).prop("disabled", true);
79
    $(btn).html(
80
        '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>' + text
81
    );
82
}
83
84
85
function saveBussinessDocument(btn)
86
{
87
    //Set the save button as loading
88
    setLoadingButton(btn,'Guardando...');
89
90
    var data = {};
91
    $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
92
        data[value.name] = value.value;
93
    });
94
    data['ncftipopago'] = $('form #ncftipopago').val();
95
    data['ncftipomovimiento'] = $('form #ncftipomovimiento').val();
96
    data.action = "save-document";
97
    data.lines = getGridData();
98
99
    $.ajax({
100
        type: "POST",
101
        url: businessDocViewUrl,
0 ignored issues
show
Bug introduced by
The variable businessDocViewUrl seems to be never declared. If this is a global, consider adding a /** global: businessDocViewUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
102
        dataType: "text",
103
        data: data,
104
        success: function (results) {
105
            if (results.substring(0, 3) === "OK:") {
106
                $("#" + businessDocViewFormName + " :input[name=\"action\"]").val('save-ok');
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
107
                $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit();
108
            } else {
109
                alert(results);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
110
                $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20));
111
            }
112
        },
113
        error: function (msg) {
114
            alert(msg.status + " " + msg.responseText);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
115
        }
116
    });
117
}
118
119
function isBusinessDocumentPage()
120
{
121
    let businessDocument = '';
122
    if ($('#formEditFacturaProveedor').length > 0) {
123
        businessDocument = 'Compra';
124
    } else if ($('#formEditFacturaCliente').length > 0) {
125
        businessDocument = 'Venta';
126
    }
127
    return businessDocument;
128
}
129
130
131
$(document).ready(function () {
132
    let tipoOperacion = isBusinessDocumentPage();
133
    let varCodSubtipoDoc = $("#doc_codsubtipodoc");
134
    if (varCodSubtipoDoc.val() !== '' && tipoOperacion !== '') {
135
        verificarCorrelativoNCF($("#doc_codsubtipodoc").val(), tipoOperacion);
136
    }
137
138
    varCodSubtipoDoc.change(function () {
139
        logConsole(varCodSubtipoDoc.val(),"#doc_codsubtipodoc val");
140
        if (tipoOperacion !== '') {
141
            verificarCorrelativoNCF(varCodSubtipoDoc.val(), tipoOperacion);
142
        }
143
    });
144
145
    // $("#doc_codsubtipodoc").change(function() {
146
    //     logConsole($("#doc_codsubtipodoc").val(),"#doc_codsubtipodoc val");
147
    //     if(ArrayTipoNCFCompras.includes($("#doc_codsubtipodoc").val())) {
148
    //         verificarCorrelativoNCF($("#doc_codsubtipodoc").val());
149
    //     }
150
    // });
151
});